﻿/******************************************************   
创建者: 文忠
创建时间: 2017-03-31
修改:
修改时间: 
功能: 商品企划使用到的基本数据(类别、系列、商品层、品类、小品类)
 ******************************************************/
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.Helper;
using NHibernateVertica;
using System.Collections.Generic;
using System.ComponentModel;
using com.nm.library.Helper;

namespace PlanAPI
{
    [Description("商品企划使用到的基本数据(类别、系列、商品层、品类、小品类)")]
    public class BaseInfoHandler : BaseHandler
    {
        public VerticaDataHelper Helper { get; set; } = new VerticaDataHelper();

        [MustLogin]
        [Description("获取基本数据类型:企划中使用")]
        [ReturnType(typeof(IList<BaseInfoResult>))]
        public ApiResult GetDropDownList([ParamDescription("数据类型:1-类别;2-系列;3-商品层;4-品类")]int type = 0)
        {

            if (!type.IsBetween(0, 5)) return Error("无效的数据类型");
            var sql = $@"SELECT  ConName ,
                        ConID
                        FROM    ngoods.ngoods_PL_PlanMatConSet
                        WHERE   MatConFlag = {type} AND AllowUsed = 0
                        and CompanyID='{com.nm.library.UserInfo.UserCompanyId}' 
                        and ExtrSource='{com.nm.library.UserInfo.UserExtrSource}'
                        ORDER BY ConID";
            var cmd = Helper.GetSqlStringCommand(sql);
            var dt = Helper.ExecuteDataTable(cmd);
            var list = ModelConvertHelper<BaseInfoResult>.ConvertToModel(dt);
            return Success(list);
        }

        [MustLogin]
        [Description("获取小品类:企划中使用")]
        [ReturnType(typeof(IList<ItemResult>))]
        public ApiResult GetDropDownItem([ParamDescription("品类代码")]string ModelID = "")
        {
            if (string.IsNullOrWhiteSpace(ModelID)) return Error("无效的品类代码");
            var sql = $@"SELECT ItemName,ItemID
                        FROM  ngoods.ngoods_PL_ModelItem
                        WHERE ModelID = @modelId
                        and ExtrSource=@ExtrSource
                        ORDER BY ItemID";
            var cmd = Helper.GetSqlStringCommand(sql);
            Helper.AddInParameter(cmd, "@modelId", System.Data.DbType.String, 12, ModelID);
            Helper.AddInParameter(cmd, "@ExtrSource", System.Data.DbType.String, 12, com.nm.library.UserInfo.UserExtrSource);
            var dt = Helper.ExecuteDataTable(cmd);
            var list = ModelConvertHelper<ItemResult>.ConvertToModel(dt);
            return Success(list);
        }

        [MustLogin]
        [Description("获取季节下拉框数据:企划中使用")]
        [ReturnType(typeof(IList<SeasonModel>))]
        public ApiResult GetSeasonDropDown()
        {
            var sql = $@"select SeasonID,SeasonName from nods.nods_erp_sd_mat_season  
                        where ExtrSource='{com.nm.library.UserInfo.UserExtrSource}' 
                        order by SeasonID ";
            var cmd = Helper.GetSqlStringCommand(sql);
            var dt = Helper.ExecuteDataTable(cmd);
            var list = ModelConvertHelper<SeasonModel>.ConvertToModel(dt);
            return Success(list);
        }
    }

    public class BaseInfoResult
    {
        [ParamDescription("name")]
        public string ConName { get; set; }
        [ParamDescription("value")]
        public string ConID { get; set; }
    }

    [Description("小品类")]
    public class ItemResult
    {
        [ParamDescription("name")]
        public string ItemName { get; set; }
        [ParamDescription("value")]
        public string ItemID { get; set; }
    }

    public class BaseInfoModel : BaseInfoResult
    {
        [ParamDescription("是否存在")]
        public long? isExists { get; set; }
    }

    [ParamDescription("季节")]
    public class SeasonModel
    {
        [ParamDescription("季节ID")]
        public string SeasonID { get; set; }
        [ParamDescription("季节名称")]
        public string SeasonName { get; set; }
    }
}
